package com.taobao.open.action;

import com.taobao.ump.client.meta.ActionDef;
import com.taobao.ump.client.meta.ConditionDef;
import com.taobao.ump.client.meta.ResourceDef;
import com.taobao.ump.core.service.MetaDefReadService;
import com.taobao.ump.core.service.client.MetaDefReadServiceTopClient;
import com.taobao.ump.marketing.MarketingBuilder;
import com.taobao.ump.marketing.MarketingTool;
import com.taobao.ump.marketing.MetaData;
import com.taobao.ump.marketing.ParameterValue;

public class TestUMP
{
	protected static String url = "http://gw.api.tbsandbox.com/router/rest";// 沙箱环境调用地址
	protected static String appkey = "1021221687";
	protected static String secret = "sandboxb03df5f8cd9dc92ef08bb8f25";

	MetaDefReadService metaDefReaderService = new MetaDefReadServiceTopClient(
			url, appkey, secret);
	MarketingBuilder builder = new MarketingBuilder();
	// builder.setMetaDefProvider(metaDefReaderService);
	
	// 可以定义一些常量
	String totalPriceID = "com.taobao.ump.meta.resource.getOrderPriceResource";
	String amountOverID = "com.taobao.ump.meta.condition.amountOver";
	String decreaseID = "com.taobao.ump.meta.action.decreaseMoney";

	// 计算总价的资源型营销积木块
	ResourceDef totalPriceDef = (ResourceDef) metaDefReaderService.getMetaDef(
			totalPriceID).getDefaultModel();
	MetaData<ResourceDef> totalPrice = builder.bind(totalPriceDef,
			new ParameterValue[] { builder.newConstParameter(true), });

	// 计算总价满XX元的的条件型营销积木块
	ConditionDef amountOverDef = (ConditionDef) metaDefReaderService
			.getMetaDef(amountOverID).getDefaultModel();

	// 计算减去XX元的的行为型营销积木块
	ActionDef decreasDef = (ActionDef) metaDefReaderService.getMetaDef(
			decreaseID).getDefaultModel();

	// 以下代码是利用计算总价的条件型营销积木块和计算减去XX元的的行为型营销积木块来组装成一个"满100元"的条件积木块
	MetaData<ConditionDef> amountOverCondition = builder.bind(amountOverDef,
			new ParameterValue[] { builder.newResourceParameter(totalPrice),
					builder.newConstParameter(100),
					builder.newConstParameter(false) });

	// 营销行为：减现金,可以将builder.newConstParameter(20)，变成builder.newUndefineParameter("amount")，
	// 从而允许卖家在参与活动时自行设置减去的钱数，上面的"满300元"也可以同样设置，变成"满xxxx"元
	MetaData<ActionDef> decreaseAction = builder.bind(decreasDef,
			new ParameterValue[] { builder.newConstParameter(20) });
	// 拼装：交易前满*元减*元
	MetaData<ActionDef> decreaseOperation = builder.bind(amountOverCondition,
			decreaseAction);
	MarketingTool marketingTool = builder.createMarketingTool();
	// marketingTool.setOperationMeta(decreaseOperation);

}
